/*
w związku z tym że Twój implementował runnable mam problem jak to połączyć.
kombinowałam, ale nie wiem co to da...
*/

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

import javax.swing.*;
/**
 *
 * @author Asia
 */
public class ClientS extends Frame implements ActionListener{
    
    String hostname;
	int port;
	private static Socket client = null;
	static PrintWriter out;
	static private boolean isOn;
	static BufferedReader line;
    
    JFrame frame1;
    JButton Accept;
    JLabel sNumber, sPort, sHost, sInitialCash;
    JTextField Number, Port, Host, InitialCash;
    int n,a,b,c;
    /**
     * Creates new form NewJFrame
     */
    public ClientS() {
        
        frame1=new JFrame();
        frame1.setTitle("Client Poker");
        frame1.setSize(400,400);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame1.setLayout(null);
        
        sNumber=new JLabel("Select Number of players");
        sNumber.setBounds(50,20,150,20);
        frame1.add(sNumber);
        
        Number=new JTextField("");
        Number.setBounds(250,20,150,20);
        frame1.add(Number);
        
        sPort=new JLabel("Select Port");
        sPort.setBounds(50,70,150,20);
        frame1.add(sPort);
        
        Port=new JTextField("");
        Port.setBounds(250,70,150,20);
        frame1.add(Port);
        
        sHost=new JLabel("Select Host");
        sHost.setBounds(50,120,150,20);
        frame1.add(sHost);
        
        Host=new JTextField("");
        Host.setBounds(250,120,150,20);
        frame1.add(Host);
        
        sInitialCash=new JLabel("Select Initial Cash");
        sInitialCash.setBounds(50,170,150,20);
        frame1.add(sInitialCash);
        
        InitialCash=new JTextField("");
        InitialCash.setBounds(250,170,150,20);
        frame1.add(InitialCash);
        
        Accept=new JButton("Akceptuj rozmiar");
        Accept.setBounds(130,250,150,20);
        frame1.add(Accept);
        Accept.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)
            {    
                Thread therad=new Thread(){
                //tu zwątkować
                //podobno można jeszcze tak
                //EventQueue.invokeLater(new Runnable() {
                try
                {
                n=Integer.parseInt(Number.getText());
                a=Integer.parseInt(Port.getText());
                b=Integer.parseInt(Host.getText());
                c=Integer.parseInt(InitialCash.getText());
                
                listenSocket(b, Port.getText());
                out.println("polaczano");
            
                Port.setEditable(false);
                Host.setEditable(false);
                InitialCash.setEditable(false);
                Number.setEditable(false);
                
                if(n>=1 && n<=5)
                    {
                    TFrame TableFrame=new TFrame();
                    TableFrame.setVisible(true);
                    }
                else
                    {
                    JOptionPane.showMessageDialog(null, "Wrong number. You can choose between 1-5");
                    }
                
                @Override
                	public void run()
                	{
                		String response = "";
                		try 
            			{
                			while (((response = in.readLine()) != null) && (isOn)) {
            				System.out.println(response);
            					if (response.indexOf("***Bye") != -1) {
            					isOn = false;
            					
            					try {
            						in.close();
            						out.close();
            						client.close();
            						} 
            					catch (IOException e) {
            						System.out.println("Error while closing streams");
            						}   					
            					}
                			}           		
            			} 
                		catch (IOException e) {}
                	}
                
                }
            catch(NumberFormatException ex){
                JOptionPane.showMessageDialog(null, "Wrong Data");} 
                } 
                thread.start();
            }
        });
        frame1.setVisible(true);
    }
    
    Client(String hostname, int port) {

		this.hostname = hostname;
		this.port = port;
	}
       
    public static void main(String[] args)
    {
        ClientS frame = new ClientS(hostname, port);
        frame.pack();
        frame.setVisible(true);
        frame.setResizable(false);
        
		client.setup();
		s.close();
    }
    
    private void connect()
    {		
		System.out.println("Trying to connect to server " + this.hostname + " on port " + port);

		try {
			client = new Socket(hostname, port);
			out = new PrintWriter(client.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					client.getInputStream()));
			line = new BufferedReader(new InputStreamReader(System.in));
			} 
		catch (UnknownHostException e) {
			System.out.println(hostname + "is not found");
			return;} 
		catch (IOException e3) {
			System.out.println("Cannot connect");
			System.exit(0);}

		System.out.println("Connected");
		isOn = true;
	}
    
    public void setup() 
    {
		this.connect();
		System.out.println("Creating new listening thread");
		new Thread(new Client(hostname, port)).start();
		System.out.println("Testing connection");
		out.println("1111");

		if (client != null && out != null && in != null) 
		{
			try {
				while (isOn) {
					out.println(line.readLine().trim());}
				} 
			catch (IOException e) {
				System.out.println("Error reading this line");
				System.exit(0);}
		}
	}

   /* NetBeans Wyrzucał że mu tego potrzeba 
    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }*/
}
